package com.ddtech.business.train.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ddtech.business.constant.BusinessEnum;
import com.ddtech.business.constant.Del;
import com.ddtech.business.train.entity.TrainPlanChapter;
import com.ddtech.business.train.entity.TrainPlanResource;
import com.ddtech.business.train.mapper.TrainPlanResourceMapper;
import com.ddtech.business.train.service.TrainPlanResourceService;
import com.ddtech.exception.BizPreconditions;
import com.ddtech.utils.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author bin
 */
@Service
public class TrainPlanResourceServiceImpl extends ServiceImpl<TrainPlanResourceMapper, TrainPlanResource> implements TrainPlanResourceService {
    @Override
    public void saveResource(TrainPlanChapter chapter) {
        LambdaQueryWrapper<TrainPlanResource> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TrainPlanResource::getChapterId, chapter.getId());
        List<TrainPlanResource> courseResourceListInfo = this.list(queryWrapper);
        List<TrainPlanResource> courseResourceList = chapter.getTrainResourceList();
        //courseResourceListInfo-courseResourceList
        List<TrainPlanResource> collect = courseResourceListInfo.stream().filter(item -> !courseResourceList.contains(item)).collect(Collectors.toList());
        for (TrainPlanResource courseResource : collect) {
            courseResource.baseDelete();
            this.saveOrUpdate(courseResource);
        }
        courseResourceList.forEach(resource -> {
            resource.setChapterId(chapter.getId());
            resource.init();
            this.saveOrUpdate(resource);
        });
    }

    @Override
    public List<TrainPlanResource> getResource(String chapterId) {
        LambdaQueryWrapper wrapper = Wrappers.<TrainPlanResource>lambdaQuery()
                .eq(TrainPlanResource::getChapterId, chapterId)
                .eq(TrainPlanResource::getIsDelete, Del.FALSE.getStatus())
                .orderByAsc(TrainPlanResource::getSort);
        return this.list(wrapper);
    }

    /***
     * description: PC端查询课程资源
     * @param:  * @param chapterId
     * @date: 2023-06-29
     * @return: java.util.List<com.ddtech.business.train.entity.TrainPlanResource>
     * @version v1.0
     * @author:wei_zhang(xiao)
     */
    @Override
    public List<TrainPlanResource> getResourceList(String chapterId) {
        LambdaQueryWrapper wrapper = Wrappers.<TrainPlanResource>lambdaQuery()
                .eq(TrainPlanResource::getChapterId, chapterId)
                .eq(TrainPlanResource::getIsDelete, Del.FALSE.getStatus())
                .orderByAsc(TrainPlanResource::getSort);
        List<TrainPlanResource> trainPlanResourceList = this.list(wrapper);
        for(TrainPlanResource trainPlanResource:trainPlanResourceList){
            String file=trainPlanResource.getResourceFile();
            if(StringUtils.isNotEmpty(file)){
                JSONArray array = JSON.parseArray(file);
                if (array!=null){
                    JSONObject json =array.getJSONObject(0);
                    trainPlanResource.setResourceFile(json.getString("url"));
                }
            }
        }
        return trainPlanResourceList;
    }

    @Override
    public void removeResource(TrainPlanChapter courseChapter) {
        LambdaQueryWrapper<TrainPlanResource> removeQuery = new LambdaQueryWrapper<>();
        removeQuery.eq(TrainPlanResource::getChapterId, courseChapter.getId());
        List<TrainPlanResource> courseResourceList = this.list(removeQuery);
        if (CollUtil.isNotEmpty(courseResourceList)) {
            courseResourceList.forEach(item -> {
                item.baseDelete();
                this.saveOrUpdate(item);
            });
        }
    }

    @Override
    public TrainPlanResource findOne(String id) {
        TrainPlanResource trainPlanResource = this.getById(id);
        if(trainPlanResource!=null){
            String obj=trainPlanResource.getResourceFile();
            if(StringUtils.isNotEmpty(obj)){
                JSONArray array = JSON.parseArray(obj);
                if (array!=null && array.size()>0){
                    JSONObject json=array.getJSONObject(0);
                    trainPlanResource.setResourceFile(json.getString("url"));
                }
            }
        }
        BizPreconditions.check(trainPlanResource == null, BusinessEnum.QUERY_ERROR.getMsg());
        return trainPlanResource;
    }
}
